In [1]:
import numpy as np
from cStringIO import StringIO
import matplotlib.pyplot as plt
import caffe
from IPython.display import clear_output, Image, display
import cv2
import PIL.Image
import os
In [2]:
os.chdir("start_deep/")
In [3]:
caffe.set_mode_cpu()
In [4]:
caffe.set_device(0)
caffe.set_mode_gpu()
In [4]:
net = caffe.Net('deploy.prototxt', "facenet_iter_200000.caffemodel", caffe.TEST)
#solver = caffe.SGDSolver('facenet_solver.prototxt')
#test_net = solver.testnets[0]
In [5]:
def showarray(a, fmt='jpeg'):
a = np.uint8(np.clip(a, 0, 255))
f = StringIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
In [6]:
#im = np.array(PIL.Image.open('train_images/0/137021_102_88_72_72.pgm'))/256.0
im = np.array(PIL.Image.open('train_images/1/image000619.pgm'))/256.0
im_input = im[np.newaxis, np.newaxis, :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = im
showarray(im)
print (len(im))
print (len(im[0]))
print (im)
output = net.forward()
print(output)
if output['prob'][0][0] >0.9:
print "visage"
In [ ]:
In [7]:
print cv2.__version__
In [8]:
img = cv2.imread('test_vis.jpg')
In [9]:
lower_res = cv2.pyrDown(img)
even_lower_res = cv2.pyrDown(lower_res)
tiny_res = cv2.pyrDown(even_lower_res)
ridiculous_res = cv2.pyrDown(tiny_res)
we_need_to_go_deeper = cv2.pyrDown(ridiculous_res)
In [10]:
plt.axis("off")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
plt.show()
In [10]:
im = cv2.imread('test_vis2.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im = cv2.pyrDown(im)
im = cv2.pyrDown(im)
imbase = im
scale = 1
while len(im) >=36*2 and len(im[0]) >36*2:
im = cv2.pyrDown(im)
scale*=2
print len(im)
showarray(im)
w = len(im)
h = len(im[0])
i = 0
j = 0
while i < w-36:
while j < h-36:
imtmp = np.array(im [i:i+36, j:j+36]/256.0)
im_input = imtmp[np.newaxis, np.newaxis, :, :]
net.blobs['data'].reshape(*im_input.shape)
net.blobs['data'].data[...] = imtmp
output = net.forward()
if output['prob'][0][1] > 0.99:
if output['prob'][0][0] < 0.001:
"""print output
print "visage trouvé @ %i , %i"%(i, j)
showarray(imtmp*255)"""
for x in range (i*scale, (i+36)*scale):
imbase[x][j*scale]=255
imbase[x][(j+36)*scale]=255
for y in range(j*scale, (j+36)*scale):
imbase[i*scale][y]=255
imbase[(i+36)*scale][y]=255
else:
#print "visage found but discarded due to low confidence"
pass
j+=2
i+=2
j = 0
showarray(imbase)
In [21]:
display(Image('cat_other.jpg'))
In [ ]:
In [ ]: